<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/10
 * Time: 11:16
 */
namespace app\api\controller;
use think\Db;
class Envelopes extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
        //发红包
    public function index(){
        extract(input());
        $price = input('param.price');
        $num = input('param.num');
        $content = input('param.content');
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        if($price>$user_balance){
            return  json(array('status'=>100,'msg'=>"金币不足,请先充值"));
        }
        $data['envelopes_price'] = $price;
        $data['surplus_price'] = $price;
        $data['envelopes_num'] = $num;
        $data['envelopes_time'] = date('Y-m-d H:i:s',time());
        $data['envelopes_content'] = $content;
        $data['user_id'] = $user_id;
        $data['packet'] = json_encode($this->open_float_red($price,$num));
        $data['num'] = 0;
        $res = Db::table('up_envelopes')->insertGetId($data);
        //更新男性用户消费记录  todo
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_consume',$price);

        if($res){
            return  json(array('status'=>200,'msg'=>$res));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //抢红包
    public function rob(){
        extract(input());
        $envelope = Db::table('up_envelopes')->where('envelopes_id',$envelopes_id)->find();
        $red_array = json_decode($envelope['packet'],true);
        if($envelope['num'] + 1 >= $envelope['envelopes_num']){
            return  json(array('status'=>100,'msg'=>"红包已领完"));
        }else{
            $map['envelopes_id'] = $envelopes_id;
            $map['user_id'] = $user_id;
            $result = Db::table('up_one_red_package')->where($map)->find();
            if($result['one_red_package_id']!=''){
                return  json(array('status'=>100,'msg'=>"您已经领取过红包"));
            }
            $user_gender = Db::table('up_u_user')->where('user_id',$user_id)->value('user_gender');
            if($user_gender==1){
                return  json(array('status'=>100,'msg'=>"男性用户不可以领取红包"));
            }
            if($envelope['num']==$envelope['envelopes_num']){
                //如果发的红包数量和领的红包数量相同的情况下，证明红包已经被领完
                Db::table('up_envelopes')->where('envelopes_id',$envelopes_id)->setField('envelopes_status',2);
            }
            //红包表num++
            Db::table('up_envelopes')->where('envelopes_id',$envelopes_id)->setInc('num',1);
            //用户拿钱，入记录，算余额
            $user_get = $red_array[$envelope['num']];//用户领取红包的金额
            //用户更新余额
             Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_balance',$user_get);
            //插入抢红包记录表
            $data['user_id'] = $user_id;
            $data['price'] = $user_get;
            $data['envelopes_id'] = $envelopes_id;
            $data['get_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_one_red_package')->insertGetId($data);
            //更新红包表的余额
             Db::table('up_envelopes')->where('envelopes_id',$envelopes_id)->setDec('surplus_price',$user_get);
            //女性用户入版单积分 todo
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_integral',$user_get);
            return  json(array('status'=>200,'msg'=>$user_get));
        }
    }

    function open_float_red($total, $num) {
        $min=0.01;//每个人最少能收到0.01元
        $sub_arr = [];
        for ($i=1;$i<$num;$i++)
        {
            $safe_total=($total-($num-$i)*$min)/($num-$i);//随机安全上限
            $money=mt_rand($min*100,$safe_total*100)/100;
            $total = $total-$money;
            //echo '第'.$i.'个红包：领'.$money.' 元，余额：'.$total.' 元 <br/>';
            $sub_arr[] = $money;
        }
        //echo '00第'.$num.'个红包：'.$total.' 元，余额：0 元';
        array_push($sub_arr, $total);
        return $sub_arr;
    }

    //抢红包记录表
    public function robList(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $envelopes_id = input('param.$envelopes_id');
        $where['a.envelopes_id'] = $envelopes_id;
        $list = Db::table('up_one_red_package')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('a.*,uuu.user_birthday')
            ->limit($limit,$num)
            ->select();
        foreach ($list as $key=>&$vo){
            $vo['age'] = $this->getAge($vo['user_birthday']);
        }
        return  json(array('status'=>200,'msg'=>$list));

    }
}